package com.example.demo.controller;

import com.example.demo.dao.DupDao;
import com.example.demo.extra.PageRequest;
import com.example.demo.entity.Dup;
import com.example.demo.service.DupService;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Dup)表控制层
 *
 * @author makejava
 * @since 2022-02-15 14:04:32
 */
@RestController
@RequestMapping("dup")
public class DupController {
    /**
     * 服务对象
     */
    @Resource
    private DupService dupService;

    @Resource
    private DupDao dupDao;

    /**
     * 分页查询
     *
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<List<Dup>> queryByPage(PageRequest pageRequest) {

        return ResponseEntity.ok(this.dupService.queryByPage(pageRequest));
    }

    @GetMapping("/stream")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<List<Dup>> list(){
        Cursor<Dup> dups = dupService.streamQuery(100);
        dups.forEach(System.out::println);

        return ResponseEntity.ok(null);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Dup> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.dupService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param dup 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Dup> add(Dup dup) {
        return ResponseEntity.ok(this.dupService.insert(dup));
    }

    /**
     * 编辑数据
     *
     * @param dup 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Dup> edit(Dup dup) {
        return ResponseEntity.ok(this.dupService.update(dup));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.dupService.deleteById(id));
    }

}

